﻿using BizBrowser.RPT.PrintEntity;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BizBrowser.RPT
{
    public partial class PrintSetting : Form
    {
        public PrintResult result = new PrintResult();
        string baseDir = Path.Combine(Application.StartupPath, "reports");
        private string tempName = "";
        private string hfJson = null;
        private String detailJson = null;
        private FastReport.Report report = new FastReport.Report();

        private PrintSetting()
        {
            InitializeComponent();
        }

        public PrintSetting(String tempName, String hfJson, String detailJson)
        {
            this.tempName = tempName;
            this.hfJson = hfJson;
            this.detailJson = detailJson;
            InitializeComponent();
        }

        

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PrintSetting_Load(object sender, EventArgs e)
        {
            List<String> list = PrintUtil.GetPrintList();
            this.printList.DataSource = list;
            this.printList.SelectedItem = PrintUtil.GetValue(tempName);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String printerName = (String)this.printList.SelectedItem;
            var reportFile = Path.Combine(baseDir, tempName);
            try
            {
                report.Load(reportFile);
                List<RptHeaderFooter> data = JsonUtil.parseHeaderFooter(hfJson);
                if (data != null)
                {
                    report.RegisterData(data, Constant.H_F_NAME);
                }
                List<RptDetail> detailData = JsonUtil.parseDetail(detailJson);
                if (detailData != null)
                {
                    report.RegisterData(detailData, Constant.DETAIL_NAME);
                }
                report.PrintSettings.Printer = printerName;
                report.PrintSettings.ShowDialog = false;
                report.PrintPrepared();
                report.Print();
                this.DialogResult = DialogResult.OK;
                this.result.printCount = 1;
                this.result.status = "success";
                this.result.printName = printerName;
                this.Close();
                PrintUtil.SetValue(tempName, printerName);
            }catch (Exception ex)
            {
                Console.WriteLine(ex);
                MessageBox.Show("加载报表失败:"+ex.Message);
            }
        }

        public String getJsonReuslt()
        {
            return JsonConvert.SerializeObject(result);
        }

        public void RegisterData(IEnumerable data, String name)
        {
            report.RegisterData(data, name);
        }
    }
}
